/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ContestFrm.java
 *
 * Created on 2011-11-10, 9:33:42
 */
package com.edu.kamai.client;

import com.edu.kamai.config.Constant;
import java.util.HashMap;

/**
 *
 * @author xyzhu
 */
public class ContestFrm extends javax.swing.JFrame {

    /** Creates new form ContestFrm */
    public ContestFrm(String sn,HashMap<Integer,String> maps,
            HashMap<Integer,Integer> mapID,String title,int cID,StudentMain sm) {
        sutdentName = sn;
        papers = maps;
        paperID = mapID;
        titleLabel = title;
        contestID = cID;
        studentfrm = sm;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LContestTitle = new javax.swing.JLabel();
        B_SPaper = new javax.swing.JButton();
        B_LPaper = new javax.swing.JButton();
        B_RPaper = new javax.swing.JButton();
        B_WPaper = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        B_Back = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        LContestTitle.setText("T:");
        if(titleLabel != ""){
            LContestTitle.setText( titleLabel );
        }

        B_SPaper.setText("Speaking");
        B_SPaper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_SPaperActionPerformed(evt);
            }
        });

        B_LPaper.setText("Listening");
        B_LPaper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_LPaperActionPerformed(evt);
            }
        });

        B_RPaper.setText("Reading");
        B_RPaper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_RPaperActionPerformed(evt);
            }
        });

        B_WPaper.setText("Writing");
        B_WPaper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_WPaperActionPerformed(evt);
            }
        });

        jButton4.setText("Submit");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        B_Back.setText("Back");
        B_Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_BackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(LContestTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(B_WPaper, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(B_RPaper, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(B_SPaper, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(B_LPaper, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(B_Back, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(12, 12, 12)))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(LContestTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(B_SPaper)
                .addGap(26, 26, 26)
                .addComponent(B_LPaper)
                .addGap(28, 28, 28)
                .addComponent(B_RPaper)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(B_WPaper)
                    .addComponent(jButton4))
                .addGap(18, 18, 18)
                .addComponent(B_Back)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void showMessage(int ans,String msg){
        if(ans == 1){
            javax.swing.JOptionPane.showMessageDialog(this, msg, "Done", ans);
        }else{
            javax.swing.JOptionPane.showMessageDialog(this, msg, "Error", ans);
        }
    }
    
    private String judgePaperPath(int rid){
        String pathValue = papers.get(rid);
        if(pathValue == null){
            showMessage(0,"sorry, this contest does not contain the " +
                    Constant.paperRoleTable[rid] + " paper");
            return null;
        }
        return pathValue;
    }
    
    private void B_SPaperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_SPaperActionPerformed
        // TODO add your handling code here:
        String path = judgePaperPath(Constant.SPEAKING_PAPER_ID);
        if(path == null){
            return;
        }
        int pid = paperID.get(Constant.SPEAKING_PAPER_ID);
        new SpeakingFrm(this,sutdentName,path,pid,contestID).setVisible(true);
        this.setVisible(false);
        
    }//GEN-LAST:event_B_SPaperActionPerformed

    private void B_WPaperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_WPaperActionPerformed
        // TODO add your handling code here:
        String path = judgePaperPath(Constant.WRITING_PAPER_ID);
        if(path == null){
            return;
        }
        int pid = paperID.get(Constant.WRITING_PAPER_ID);
        new WritingFrm(this,sutdentName,path,pid,contestID).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_B_WPaperActionPerformed

    private void B_LPaperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_LPaperActionPerformed
        // TODO add your handling code here:
        String path = judgePaperPath(Constant.LISTENING_PAPER_ID);
        if(path == null){
            return;
        }
    }//GEN-LAST:event_B_LPaperActionPerformed

    private void B_RPaperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_RPaperActionPerformed
        // TODO add your handling code here:
        String path = judgePaperPath(Constant.READING_PAPER_ID);
        if(path == null){
            return;
        }
        int pid = paperID.get(Constant.READING_PAPER_ID);
        new ReadingFrm(this,sutdentName,path,pid,contestID).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_B_RPaperActionPerformed

    private void B_BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_BackActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        studentfrm.setVisible(true);
    }//GEN-LAST:event_B_BackActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        
        
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ContestFrm(null,null,null,null,0,null).setVisible(true);
            }
        });
    }
    
    private StudentMain studentfrm;
    private HashMap<Integer,String> papers;
    private HashMap<Integer,Integer> paperID;
    private int contestID;
    private String titleLabel;
    private String sutdentName = "test";
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton B_Back;
    private javax.swing.JButton B_LPaper;
    private javax.swing.JButton B_RPaper;
    private javax.swing.JButton B_SPaper;
    private javax.swing.JButton B_WPaper;
    private javax.swing.JLabel LContestTitle;
    private javax.swing.JButton jButton4;
    // End of variables declaration//GEN-END:variables
}
